package com.zoffy.wmsbackend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoffy.wmsbackend.mapper.ChartMapper;
import com.zoffy.wmsbackend.model.entity.ReportDaySummaryStockIn;
import com.zoffy.wmsbackend.model.entity.ReportDaySummaryStockOut;
import com.zoffy.wmsbackend.model.vo.report.SummaryDayStockIn;
import com.zoffy.wmsbackend.model.vo.report.SummaryDayStockInData;
import com.zoffy.wmsbackend.model.vo.report.SummaryDayStockOut;
import com.zoffy.wmsbackend.model.vo.report.SummaryDayStockOutData;
import com.zoffy.wmsbackend.service.ReportService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private ChartMapper chartMapper;

    @Override
    public List<SummaryDayStockIn> getSummaryDayStockIn(String endTimeStr, int deltaTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date endDate = sdf.parse(endTimeStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endDate);
            calendar.add(Calendar.DATE, -deltaTime+1);
            Date startDate = calendar.getTime();
            List<SummaryDayStockIn> res = chartMapper.getSummaryDayStockIn(sdf.format(startDate),sdf.format(endDate));
            return res;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public SummaryDayStockInData getSummaryDayStockInSum(String endTime) {
        List<ReportDaySummaryStockIn> summaryDayStockInByTime = chartMapper.getSummaryDayStockInByTime(endTime);
        Integer orderNum = 0;
        Long goodsNum = 0L;
        BigDecimal goodsAmountGd = BigDecimal.ZERO;
        for (ReportDaySummaryStockIn summaryDayStockIn : summaryDayStockInByTime) {
            orderNum+= summaryDayStockIn.getOrderNum();
            goodsNum += summaryDayStockIn.getGoodsNum();
            goodsAmountGd = goodsAmountGd.add(new BigDecimal(summaryDayStockIn.getGoodsAmountGd()));
        }
        SummaryDayStockInData data=new SummaryDayStockInData();
        data.setOrderNum(orderNum);
        data.setGoodsNum(goodsNum);
        data.setGoodsAmountGd(goodsAmountGd.toString());
        return data;
    }

    @Override
    public List<SummaryDayStockOut> getSummaryDayStockOut(String endTime, int deltaTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date endDate = sdf.parse(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endDate);
            calendar.add(Calendar.DATE, -deltaTime+1);
            Date startDate = calendar.getTime();
            List<SummaryDayStockOut> res = chartMapper.getSummaryDayStockOut(sdf.format(startDate),sdf.format(endDate));
            return res;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public SummaryDayStockOutData getSummaryDayStockOutSum(String endTime) {
        List<ReportDaySummaryStockOut> summaryDayStockOutByTime = chartMapper.getSummaryDayStockOutByTime(endTime);
        Integer orderNum = 0;
        Long goodsNum = 0L;
        BigDecimal goodsAmountGd = BigDecimal.ZERO;
        for (ReportDaySummaryStockOut summaryDayStockOut : summaryDayStockOutByTime) {
            orderNum+= summaryDayStockOut.getOrderNum();
            goodsNum += summaryDayStockOut.getGoodsNum();
            goodsAmountGd = goodsAmountGd.add(new BigDecimal(summaryDayStockOut.getGoodsAmountGd()));
        }
        SummaryDayStockOutData data=new SummaryDayStockOutData();
        data.setOrderNum(orderNum);
        data.setGoodsNum(goodsNum);
        data.setGoodsAmountGd(goodsAmountGd.toString());
        return data;
    }


}
